package com.xy.smartsms.plugincmcc;

import android.content.Context;

import com.xy.smartsms.util.Log;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

import cn.cmcc.online.smsapi.SmsPortData;
import cn.cmcc.online.smsapi.TerminalApi;

import static java.util.concurrent.Executors.newFixedThreadPool;

/**
 * Created by admin on 2017/3/30.
 */

public class PublicInfoCallable implements Callable<SmsPortData> {
    private static final String TAG = PublicInfoCallable.class.getSimpleName();
    private Context context = null;
    private String phone;
    private ExecutorService executor = newFixedThreadPool(1);

    public PublicInfoCallable(Context context) {
        this.context = context;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    @Override
    public SmsPortData call() throws Exception {
        SmsPortData portData = TerminalApi.getPortDataManager().getPortInfo(context, phone);
        return portData;
    }

    public SmsPortData executeWithTimeout(int timeout) {
        SmsPortData portData = null;
        Future<SmsPortData> future = executor.submit(this);
        try {
            portData = future.get(timeout, TimeUnit.MILLISECONDS);
        } catch (Throwable t) {
            Log.e(TAG, "executeWithTimeout", t);
        }

        return portData;
    }
}
